package com.typy.www.tpay.ui.acitivity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.typy.www.tpay.R;
import com.typy.www.tpay.base.BaseBackActivity;
import com.typy.www.tpay.bean.bean.BaseReturn;
import com.typy.www.tpay.bean.listvm.IcoTypeListVm;
import com.typy.www.tpay.bean.vm.IcoTypeVm;
import com.typy.www.tpay.constance.Constance;
import com.typy.www.tpay.ui.adapter.IcotypeAdapter;
import com.typy.www.tpay.ui.prt.PtrListContent;
import com.typy.www.tpay.util.ClassHoldUtil;
import com.typy.www.tpay.util.GsonUtils;
import com.typy.www.tpay.util.ParamsUtil;
import com.typy.www.tpay.util.UiHelper;
import com.typy.www.tpay.util.UpdataUiUtil;
import com.typy.www.tpay.util.listviewanimations.IntentUtil;
import com.typy.www.tpay.volley.HTTPUtils;
import com.typy.www.tpay.volley.VolleyListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 13799 on 2018/7/11.
 */

public class IcoActivity extends BaseBackActivity {
    @BindView(R.id.rd_yure)
    RadioButton rdYure;
    @BindView(R.id.rd_jinxin)
    RadioButton rdJinxin;
    @BindView(R.id.rd_jiesu)
    RadioButton rdJiesu;
    @BindView(R.id.ll_holder)
    LinearLayout llHolder;
    PtrListContent ptrListContent;
    ListView mListView;
    IcotypeAdapter icotypeAdapter;
//    MyOnClickListener listener;
    private List<IcoTypeVm> iconTypedata = new ArrayList<>();

    public static void show(Context context) {
        IntentUtil.intent2Activity(context, IcoActivity.class);
    }

    @Override
    protected void initView() {
        super.initView();
        ButterKnife.bind(this);
        setTitle("众筹项目中心");
        ((TextView) mBtnRight).setText("记录");
        mBtnRight.setVisibility(View.VISIBLE);
        addView();
        initAdapter();
        refresh();
    }

    private void initAdapter() {
        icotypeAdapter = new IcotypeAdapter(getActivity());
        icotypeAdapter.setmType(ClassHoldUtil.ICOYURE);
        mListView.setAdapter(icotypeAdapter);
    }

    private void addView() {
        rdYure.setChecked(true);
        UpdataUiUtil.getDefaul().setDrawTop(getActivity(), R.drawable.checked_ico_yure, 70, 70, rdYure).
                setDrawTop(getActivity(), R.drawable.checked_ico_jinxing, 70, 70, rdJinxin).
                setDrawTop(getActivity(), R.drawable.checked_ico_jiesu, 70, 70, rdJiesu);

        ptrListContent = new PtrListContent(getActivity()) {
            @Override
            protected void getPage(int pageIndex, boolean refresh) {
                getIcolistDate(pageIndex, refresh);
            }
        };
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1);
        mListView = ptrListContent.getListView();
        llHolder.addView(ptrListContent.getmView(), params);
    }

    private void refresh() {
        getIcolistDate(1, true);
    }

    @Override
    protected int getContentResId() {
        return R.layout.activity_ico;
    }

    @Override
    protected void onRightButtonClick() {
        super.onRightButtonClick();
        RecordActivity.show(getActivity(),ClassHoldUtil.ICO);
    }

    //激活记录
    private void getIcolistDate(final int page, final boolean refresh) {
        ParamsUtil params = new ParamsUtil();
        params.put("pageNum", page);
        params.put("pageSize", Constance.LIMT);
        int fdType = 0;
        if (rdYure.isChecked()) {
            fdType = 1;
        } else if (rdJinxin.isChecked()) {
            fdType = 2;
        } else if (rdJiesu.isChecked()) {
            fdType = 3;
        }
        params.put("fdType", fdType);
        HTTPUtils.post(getActivity(), Constance.getIcolistURL(), params.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                if (refresh) {
                    iconTypedata.clear();
                    icotypeAdapter.setData(iconTypedata);
                    icotypeAdapter.notifyDataSetChanged();
                }
                IcoTypeListVm vm = GsonUtils.parseJSON(ret.getData().toString(), IcoTypeListVm.class);
                if (vm != null && vm.getData() != null) {
                    iconTypedata.addAll(vm.getData());
                    icotypeAdapter.setData(iconTypedata);
                    icotypeAdapter.notifyDataSetChanged();
                    ptrListContent.loadInitialPage(mListView.getCount(), vm.getCount());
                } else {
                    ptrListContent.loadInitialPage(0);
                }
            }

            @Override
            public void onResponseErr(int code, String msg) {
                UiHelper.showToast(getActivity(), msg);
                ptrListContent.loadInitialPage(0);
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.rd_yure, R.id.rd_jinxin, R.id.rd_jiesu})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rd_yure:
                icotypeAdapter.setmType(ClassHoldUtil.ICOYURE);
                refresh();
                break;
            case R.id.rd_jinxin:
                icotypeAdapter.setmType(ClassHoldUtil.ICOJINXING);
                refresh();
                break;
            case R.id.rd_jiesu:
                icotypeAdapter.setmType(ClassHoldUtil.ICOJIESU);
                refresh();
                break;
        }
    }

//    class MyOnClickListener implements View.OnClickListener {
//
//
//        @Override
//        public void onClick(View view) {
//            refresh();
//        }
//    }

}
